<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 *
 * @Date: 2017-06-08
 * @Time: 16:56
 * @Author: cdkay
 * @Email: network@iyuanma.net
 *
 * @File： Role.php
 */
class Role extends MY_Controller {
    public function __construct()
    {
        parent::__construct();

        $this->load->model('system/Role_model');
    }

    /**
     * index
     * 系统角色列表
     */
    public function index()
    {
        $data['role'] = $this->Role_model->get_all_role();

        $this->load->view('system/role/index', $data);
    }

    /**
     * add
     * 添加角色
     */
    public function add()
    {
        // 获取权限数据
        $data['auth'] = $this->Role_model->get_auth();

        $this->load->view('system/role/add', $data);
    }

    /**
     * edit
     * 编辑权限
     *
     * @param int $role_id 权限节点id
     */
    public function edit($role_id)
    {
        // 获取权限数据
        $data['auth'] = $this->Role_model->get_auth();

        $data['role'] = $this->Role_model->get_single_role($role_id);

        //如果请求编辑的是系统保留角色则立即停止操作并返回列表
        if ($data['role']['role_id']==1 && $data['role']['role_acl']=='all') redirect(site_url('system/Role/index'));

        $this->load->view('system/role/edit', $data);
    }

    /**
     * save
     * 保存权限数据 添加|编辑
     */
    public function save()
    {
        if (!IS_POST) exit();

        $result = $this->Role_model->role_save();

        exit(json_encode($result));
    }

    /**
     * del
     * 删除角色
     */
    public function del()
    {
        $result = $this->Role_model->role_delete();

        exit(json_encode($result));
    }
}